package com.hnyfkj.jyindustry.biz.systemplate.controller;

import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.hnyfkj.jyindustry.biz.systemplate.dto.SysTemplateInsertDto;
import com.hnyfkj.jyindustry.biz.systemplate.dto.SysTemplateQueryDto;
import com.hnyfkj.jyindustry.biz.systemplate.dto.SysTemplateUpdateDto;
import com.hnyfkj.jyindustry.biz.systemplate.entity.SysTemplateEntity;
import com.hnyfkj.jyindustry.biz.systemplate.service.SysTemplateService;
import com.hnyfkj.jyindustry.common.utils.PageUtils;
import com.hnyfkj.jyindustry.common.utils.Response;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zjq
 *
 * @Description
 *
 * @date 2020年12月10日
 */
@Controller
@RequestMapping("sysTemplate")
@Api(tags = { "模板信息" })
public class SysTemplateController {

	@Autowired
	private SysTemplateService sysTemplateService;

	/**
	 * 添加页面
	 */
	@GetMapping("/add")
	public String addPage() {
		return "sys_template/template-add";
	}

	/**
	 * 编辑页面
	 */
	@GetMapping("/edit")
	public String editPage(@RequestParam("id") Integer id, Model model) {
		SysTemplateEntity sysTemplateEntity = sysTemplateService.getById(id);
		sysTemplateEntity.setInfo(StringEscapeUtils.unescapeHtml4(sysTemplateEntity.getInfo()));
		model.addAttribute("sysTemplateEntity", sysTemplateEntity);
		return "sys_template/template-edit";
	}

	/**
	 * 编辑
	 */
	@PostMapping("/getTemplateById")
	@ResponseBody
	public SysTemplateEntity getTemplateById(@RequestParam("id") Integer id) {
		SysTemplateEntity sysTemplateEntity = sysTemplateService.getTemplateById(id);
		return sysTemplateEntity;
	}

	/**
	 * 详情页面
	 */
	@GetMapping("/detail")
	public String detailPage(@RequestParam("id") String id, Model model) {
		SysTemplateEntity sysTemplateEntity = sysTemplateService.getById(id);
		model.addAttribute("sysTemplateEntity", sysTemplateEntity);
		return "sys_template/template-detail";
	}
	
	/**
	 * 详情页面
	 */
	@GetMapping("/lookEditor")
	public String lookEditor(@RequestParam("id") String id, Model model) {
		SysTemplateEntity sysTemplateEntity = sysTemplateService.getById(id);
		sysTemplateEntity.setInfo(StringEscapeUtils.unescapeHtml4(sysTemplateEntity.getInfo()));
		model.addAttribute("info", sysTemplateEntity.getInfo());
		return "sys_template/template-editor";
	}

	/**
	 * 列表页面
	 */
	@GetMapping("/listPage")
	public String listPage() {
		return "sys_template/template-table";
	}

	/**
	 * 列表
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "列表查看", produces = "application/json")
	public Response<PageUtils<SysTemplateEntity>> list(@RequestBody final SysTemplateQueryDto queryDto) {
		PageUtils<SysTemplateEntity> ls = sysTemplateService.selectListDto(queryDto);
		return Response.success(ls.getCount(), ls.getList());
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ResponseBody
	@ApiOperation(value = "新增", produces = "application/json")
	public Response save(@RequestBody final SysTemplateInsertDto sysTemplate) {
		sysTemplateService.insert(sysTemplate);
		return Response.success();
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ResponseBody
	@ApiOperation(value = "修改", produces = "application/json")
	public Response update(@RequestBody final SysTemplateUpdateDto sysTemplate) {
		sysTemplateService.update(sysTemplate);
		return Response.success();
	}

	/**
	 * 删除
	 */
	@PostMapping("/delete")
	@ResponseBody
	@ApiOperation(value = "删除")
	public Response delete(@RequestParam("ids[]") final String[] memberIds) {
		sysTemplateService.delectBatch(memberIds);
		return Response.success();
	}

	/**
	 * 富文本上传图片
	 */
	@PostMapping("/ueditor")
	@ResponseBody
	public JSONObject uploadImage(@RequestParam("file") MultipartFile[] image) throws Exception {
		return sysTemplateService.uploadImage(image);
	}
	
	/**
	 * 多图片上传图片
	 */
	@PostMapping("/imageUpload")
	@ResponseBody
	public String imageUpload(@RequestParam("image")MultipartFile image) throws Exception {
		return sysTemplateService.imageUpload(image);
	}
}
